package cn.kgc.mes.feedback.controller;

import cn.kgc.mes.common.core.domain.entity.SysDept;
import cn.kgc.mes.common.core.domain.entity.SysDictData;
import cn.kgc.mes.system.mapper.SysDeptMapper;
import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

import cn.kgc.mes.common.core.plus.response.Result;
import cn.kgc.mes.common.core.plus.response.ResultUtil;
import cn.kgc.mes.common.core.plus.dto.common.CommonDataPageResponse;
import cn.kgc.mes.common.core.plus.response.StatusResponse;

import cn.kgc.mes.feedback.domain.vo.ProFeedbackVo;
import cn.kgc.mes.feedback.business.ProFeedbackBusiness;

import java.util.List;

/**
 * 生产报工记录控制器
 * 
 * @author cly
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/feedback/feedback")
public class ProFeedbackController {

    @Resource
    ProFeedbackBusiness business;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    /**
     * 分页查询生产报工记录列表
     */
//    @PreAuthorize("@ss.hasPermi('feedback:feedback:list')")
    @PostMapping("/page")
    public Result page(@RequestBody ProFeedbackVo vo) {
        CommonDataPageResponse<ProFeedbackVo> response = business.page(vo);

        return ResultUtil.success(response);
    }

    /**
     * 获取生产报工记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('feedback:feedback:query')")
    @PostMapping("/info")
    public Result info(@RequestBody ProFeedbackVo vo) {

        ProFeedbackVo info = business.info(vo);

        return ResultUtil.success(info);
    }

    /**
     * 新增生产报工记录
     */
//    @PreAuthorize("@ss.hasPermi('feedback:feedback:add')")
    @PostMapping("/add")
    public Result add(@RequestBody ProFeedbackVo vo) {
        System.out.println("后端接收的 workorderId：" + vo.getWorkorderId());
        System.out.println("后端接收的workstationId：" + vo.getWorkstationId());
        StatusResponse response = business.add(vo);
        if (response.getSuccess() == false) {
            return ResultUtil.fail(response.getMsg());
        }

        return ResultUtil.success(response);
    }

    /**
     * 修改生产报工记录
     */
//    @PreAuthorize("@ss.hasPermi('feedback:feedback:edit')")
    @PostMapping("/update")
    public Result update(@RequestBody ProFeedbackVo vo) {
        StatusResponse response = business.update(vo);
        if (response.getSuccess() == false) {
            return ResultUtil.fail(response.getMsg());
        }

        return ResultUtil.success(response);
    }

    /**
     * 删除生产报工记录
     */
//    @PreAuthorize("@ss.hasPermi('feedback:feedback:remove')")
    @PostMapping("/delete")
    public Result delete(@RequestBody ProFeedbackVo vo) {

        StatusResponse response = business.delete(vo);
        if (response.getSuccess() == false) {
            return ResultUtil.fail(response.getMsg());
        }

        return ResultUtil.success(response);
    }

    @GetMapping("getLeader")
    public Object getLeader(Long deptId){
        // 判空处理
        if (deptId == null) {
            return "部门ID不能为空";
        }

        // 创建查询条件构造器，指定实体类
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<>();
        // 添加查询条件：部门ID等于传入的deptId
        wrapper.eq(SysDept::getDeptId, deptId);
        SysDept dept = new SysDept();
        dept.setDeptId(deptId);
        try {
            // 假设你有一个SysDeptMapper接口，调用其selectList方法查询
            List<SysDept> deptList = sysDeptMapper.selectDeptList(dept);

            // 根据实际业务需求处理查询结果
            if (deptList != null && !deptList.isEmpty()) {
                // 如果只需要一个部门信息，取第一个
                return deptList.get(0);
            } else {
                return "未找到对应部门信息";
            }
        } catch (Exception e) {
            return "查询失败：" + e.getMessage();
        }
    }

}
